/**
 * Created by Administrator on 2017/4/20.
 */




var Card9Player = require('../CARD9/player').Card9Player;
var Card9Map = require('../CARD9/Card9').PatternSum;
var Card9Pattern = require('../CARD9/Card9').Pattern;
var compfun = require('./rules').ComparePatternFun;
var choicebest = require('./rules').ChoiceBest;

var Const = require('./const');
var SubCode = require('../subCode');
var Rule = require('./rules');

exports.YQWCard9Player = Card9Player.extend({

    _selectedCards: null,     // 选择的首道2张牌

    init: function (uid, msg) {
        this._super(uid, msg);

        this.firstScore = 0;
        this.secondScore = 0;
        //总结算
        this.roundTopScore = 0;
        this.topCardType = 0;
        this.winSum = 0;
        this.killAllSum = 0;
        this.loseAllSum = 0;
        //单局结算
        this._stat.roundPattern = [];
        this._selectedCards = null;
        this.robDeal = 0;
        //
        this._baseScore = 0;
        this._selectedCards = null;

        this.winNums = 0;
        this.defeated = 0;
        this.hasNiu = 0;
        this.noneNiu = 0;

        this._stat.roundPattern = [];
        this.isSelected = false;
    },
    reset:function(){

        this._selectedCards = null;
        this._stat.roundPattern = [];
        this.robDeal = 0;
    },
    getInitHandCardNeedNum:function(){
        var room = this.getRoom();
        var type = room.getSubType();
        if(type == 'FOURCARD9') {
            return 4;
        }
        if(type == 'TWOCARD9'){
            return 2;
        }
        if(type == 'GUOCARD9') {
            if (room.getOption('CardType') == 1) {
                return 2;
            }
            return 4;
        }
        logger.debug("subtypeOption err");
        return 0;
    },

    setBaseScore: function (score) {
        if(this.firstScore && this.secondScore){
            return;
        }

        if(!this.firstScore){
            this.firstScore = score;
        }else{
            this.secondScore = score;
        }

    },
    isSetBaseScore:function () {
      return this.firstScore && this.secondScore;
    },

    setSelectedCards: function (cardsArr) {
        if (!cardsArr || cardsArr.length == 0) {
            this._selectedCards = [];
            return true;
        }

        if (cardsArr.length != 2) {
            return false;
        }
        this._selectedCards = cardsArr;

    },

    isSelectedCards: function () {
        return (this._selectedCards) ? true : false;
    },

    /******************************************************************
     * 重载的接口
     ******************************************************************/

    getInfo: function () {
        var info = this._super();
        //info.remainScore = this._stat.remainScore;
        return info;
    },

    getReconnectInfo: function (isSelf) {
        var info = this._super(isSelf);

        //info.robDealer = this._robDealer;

        if (isSelf) {
            info.selectCards = this._selectedCards;
            info.handCards = this._handCards.getInitCards();
        }

        info.score = this.getScore();
        info.roundScore = this.getRoundScore();
        info.firstScore = this.firstScore;
        info.secondScore = this.secondScore;

        info.pattern = this._stat.roundPattern;

        return info;
    },

    getSettlementInfo: function () {
        var info = this._super();

        info.handCards = this._handCards.getInitCards();
        info.selectCards = this._selectedCards;
        var room = this.getRoom();
        if (room.selected) {
            if (this.getRoundScore() > 0) {
                this.winNums += 1;
            } else {
                this.defeated += 1;
            }
            if (this._stat.roundPattern > 1) {
                this.hasNiu += 1;
            } else {
                this.noneNiu += 1;
            }
        }
        info.winNums = this.winNums;
        info.defeated = this.defeated;
        info.hasNiu = this.hasNiu;
        info.noneNiu = this.noneNiu;
        info.baseScore = this.baseScore;
        return info;
    },

    settlementPattern: function () {
        // if (!this._selectedCards) {
        //     //自动选出最优三张牌
        //     var results = NN.calcNiuCards(this._handCards.getInitCards());
        //     this._selectedCards = (results.length > 0) ? results[0] : [];
        // }


        var room = this.getRoom();
        var GZ = false;
        var JWY = false;
        if(room.getSubType() != 'GUOCARD9'){
            GZ = room.getOption('GZ');
            JWY = room.getOption('GTW');
        }
        var cardNum = this._handCards.getInitCards().length;
        if(cardNum == 4){
            if(!this._selectedCards){
                var selected = choicebest(this._handCards.getInitCards(),GZ,JWY);
                var cards1 = [this._handCards.getInitCards()[selected[0]],this._handCards.getInitCards()[selected[1]]];
                var cards2 = [this._handCards.getInitCards()[selected[2]],this._handCards.getInitCards()[selected[3]]];
                cards1.sort();
                cards2.sort();
                var codecard1 = cards1[0] + 'l'+cards1[1];
                var codecard2 = cards2[0] + 'l'+ cards2[1];

                this._stat.roundPattern = [Card9Map[codecard1],Card9Map[codecard2]];
            }else{
                var temp1 = this._cardArrayToObj(this._handCards.getInitCards());
                var temp2 = this._selectedCards.slice(0);

                temp2.forEach(function(v){
                    temp1[v] -= 1;
                    if(temp1[v] < 0)
                        temp1[v] = 0;
                });
                var temp1toAr = this._cardObjToArray(temp1);

                temp1toAr.sort();
                temp2.sort();
                var codecard1 = temp1toAr[0] + 'l'+temp1toAr[1];
                var codecard2 = temp2[0] + 'l'+ temp2[1];

                this._stat.roundPattern = [Card9Map[codecard1],Card9Map[codecard2]]
            }
        }else{
            var temp = this._handCards.getInitCards().slice(0);
            temp.sort();
            var codeStr = temp[0]+'l'+temp[1];
            this._stat.roundPattern = [Card9Map[codeStr]]
        }


    },

    settlement: function () {
        this._super();
         var room = this.getRoom();
         var player = this;
         var ePlayer = room.getDealerPlayer();
        if (!ePlayer) {
            return;
        }
        if (this.uid() == ePlayer.uid()) {
            return;
        }

        var GZ = false;
        var JWY = false;
        if(room.getSubType() != 'GUOCARD9'){
            GZ = room.getOption('GZ');
            JWY = room.getOption('GTW');
        }

        if(player._stat.roundPattern.length == 1){
            var playerRoundPattern=  compfun(player._stat.roundPattern[0],GZ,JWY);
            var eplayerRoundPattern= compfun(player._stat.roundPattern[0],GZ,JWY);
            var sumscore = player.secondScore + player.firstScore;
            var firstscore = player.firstScore;
            if(playerRoundPattern < eplayerRoundPattern){
                if(playerRoundPattern <= Card9Pattern.POINTEIGHT){
                    player.addRoundScore(sumscore);
                    ePlayer.addRoundScore(-sumscore);
                }else{
                    player.addRoundScore(firstscore);
                    ePlayer.addRoundScore(-firstscore);
                }

            } else{
                if(eplayerRoundPattern <= Card9Pattern.POINTEIGHT){
                    player.addRoundScore(-sumscore);
                    ePlayer.addRoundScore(sumscore);
                }else{
                    player.addRoundScore(-firstscore);
                    ePlayer.addRoundScore(firstscore);
                }
            }
        }else{
            var playerRoundPattern1=  compfun(player._stat.roundPattern[0],GZ,JWY);
            var playerRoundPattern2=  compfun(player._stat.roundPattern[0],GZ,JWY);
            var eplayerRoundPattern1= compfun(player._stat.roundPattern[0],GZ,JWY);
            var eplayerRoundPattern2= compfun(player._stat.roundPattern[0],GZ,JWY);
            var sumscore = player.secondScore + player.firstScore;
            var firstscore = player.firstScore;

            if(playerRoundPattern1 == eplayerRoundPattern1 && playerRoundPattern2 == eplayerRoundPattern2 ){
                if(eplayerRoundPattern1 <= Card9Pattern.POINTEIGHT && eplayerRoundPattern2 <= Card9Pattern.POINTEIGHT){
                    player.addRoundScore(-sumscore);
                    ePlayer.addRoundScore(sumscore);
                }else{
                    player.addRoundScore(-firstscore);
                    ePlayer.addRoundScore(firstscore);
                }
            }else if((playerRoundPattern1 - eplayerRoundPattern1) * (playerRoundPattern2 - eplayerRoundPattern2) <= 0){
                player.addRoundScore(0);
                ePlayer.addRoundScore(0);
            }else{
                if(playerRoundPattern1 > eplayerRoundPattern1){
                    if(eplayerRoundPattern1 <= Card9Pattern.POINTEIGHT && eplayerRoundPattern2 <= Card9Pattern.POINTEIGHT){
                        player.addRoundScore(-sumscore);
                        ePlayer.addRoundScore(sumscore);
                    }else{
                        player.addRoundScore(-firstscore);
                        ePlayer.addRoundScore(firstscore);
                    }
                }else{
                    if(playerRoundPattern1 <= Card9Pattern.POINTEIGHT && playerRoundPattern2 <= Card9Pattern.POINTEIGHT){
                        player.addRoundScore(sumscore);
                        ePlayer.addRoundScore(-sumscore);
                    }else{
                        player.addRoundScore(firstscore);
                        ePlayer.addRoundScore(-firstscore);
                    }
                }
            }
        }
    },

    onStartNewRound: function () {
        this._super();
        this.reset();
    },

    /******************************************************************
     * 重载的接口
     ******************************************************************/

    /******************************************************************
     * 私有功能函数
     ******************************************************************/


    _cardArrayToObj: function (arrayCards) {
        var obj = {};
        arrayCards.forEach(function (v) {
            if (obj[v]) {
                obj[v] += 1;
            } else {
                obj[v] = 1;
            }
        });
        return obj;
    },
    _cardObjToArray: function (arrayObj) {
        var keys = Object.keys(arrayObj);
        var temp = [];
        keys.forEach(function (v) {
            if (arrayObj[v]) {
                temp.push(v);
            }
        });
        return temp;
    },
    // getInitHandCardPreCards: function () {

    //     var preCards = [];
    //     if (this._prehandcards) {
    //         for (var i = 0; i < this._prehandcards.length; i++) {
    //             preCards.push(this._prehandcards[i]);
    //         }
    //     }
    //     var distance = this.getRoom().getIndexDistance(this.getIndex(), this.getRoom().getDealerIndex());
    //     console.log("distanceee")
    //     console.log(distance);
    //     if (distance == 0) {
    //         preCards.push(202);
    //         preCards.push(102);
    //         preCards.push(104);
    //         preCards.push(107);
    //         preCards.push(108);



    //     } else if (distance == 1 || distance == 4 || distance == 2) {
    //         preCards.push(302);
    //         preCards.push(402);
    //         preCards.push(204);
    //         preCards.push(207);
    //         preCards.push(208);


    //     }
    //     //else if (distance == 2 || distance == 5) {
    //     // preCards.push(201);
    //     // preCards.push(202);
    //     // preCards.push(203);
    //     // preCards.push(204);
    //     // preCards.push(205);
    //     // 102,202,302,402,209
    //     //}
    //     var room = this.getRoom();
    //     logger.debug("room=%d noPreCards %j,cards = %j", room.rid(), preCards, this._prehandcards);

    //     return preCards;
    // },
});